<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-15
 * Time: 15:29:15
 * Info:
 */

namespace plugin\huiblog\app\common\taglib;

use think\template\TagLib;

class HuiBlog extends TagLib
{

    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（1-闭合、0-开放） alias 标签别名 level 嵌套层次

        //幻灯轮播
        'slides'  => ['attr' => 'slide_id,num,order,item', 'close' => 1],//非必须属性item
        //热门标签
        'hottag'  => ['attr' => 'num,order,item', 'close' => 1],
        //友情链接
        'links'   => ['attr' => 'num,order,logo,item', 'close' => 1],
        //博主动态
        'actions' => ['attr' => 'num,item', 'close' => 1],
        //分页
        'pages'   => ['attr' => '', 'close' => 0],
        //栏目分类【是cate，非导航nav】
        'cates'   => ['attr' => 'num,order,item', 'close' => 1],
        //文章列表
        'lists'   => ['attr' => 'type,cate_id,is_home,thumb,num,order,item,page', 'close' => 1],
        //文章搜索
        'search'  => ['attr' => 'type,cate_id,is_home,thumb,num,order,item,page', 'close' => 1],

    ];

    /**
     * 幻灯片标签
     */
    public function tagSlides($tag, $content)
    {
        $slide_id = $tag['slide_id'] ?? 0;
        $num      = $tag['num'] ?? 3;
        $order    = $tag['order'] ?? "sort asc";
        $item     = $tag['item'] ?? 'vo';//循环变量名

        if (strpos($slide_id, '$') === 0) {
            $this->autoBuildVar($slide_id);
        }

        $parse = <<<parse
        <?php
            \$__SLIDE_ITEMS__ = (new plugin\slide\app\model\Slide())->slides({$slide_id},{$num},"{$order}");
        ?>
        {foreach name="__SLIDE_ITEMS__" id="{$item}"}
        {$content}
        {/foreach}
        parse;

        return $parse;

    }

    //热门标签
    public function tagHottag($tag, $content)
    {
        $num   = $tag['num'] ?? 20;
        $order = $tag['order'] ?? "sort asc";    //如果随机，轻直接输入 rand()
        $item  = $tag['item'] ?? 'vo';//循环变量名

        $parse = <<<parse
        <?php
            \$__HOTTAG_ITEMS__ = (new plugin\huiblog\app\model\Keywords())->tags({$num},"{$order}");
        ?>
        {foreach name="__HOTTAG_ITEMS__" id="{$item}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    //友情链接
    public function tagLinks($tag, $content)
    {
        $num   = $tag['num'] ?? 20;
        $order = $tag['order'] ?? "sort asc";
        $item  = $tag['item'] ?? 'vo';//循环变量名
        $logo  = $tag['logo'] ?? 0; //是否只获取带logo的

        $parse = <<<parse
        <?php
            \$__LINKS_ITEMS__ = (new plugin\huiblog\app\model\Links())->getList({$num},{$logo},"{$order}");
        ?>
        {foreach name="__LINKS_ITEMS__" id="{$item}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    //博客分类
    public function tagCates($tag, $content)
    {
        $item  = $tag['item'] ?? 'vo';//循环变量名
        $order = $tag['order'] ?? "sort asc";

        $parse = <<<parse
        <?php
            \$__CATES_ITEMS__ = (new plugin\huiblog\app\model\ArticleCate())->cates('{$order}');
        ?>
        {foreach name="__CATES_ITEMS__" id="{$item}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    //博主动态
    public function tagActions($tag, $content)
    {
        $num  = $tag['num'] ?? 8;
        $item = $tag['item'] ?? 'vo';//循环变量名
        $page = $tag['page'] = $tag['page'] ?? 0;//是否开启分页

        $parse = <<<parse
        <?php
            \$is_page = $page;
            \$__ACTIONS_ITEMS__ = (new plugin\huiblog\app\model\Action())->lists({$num},{$page});
        ?>
        {foreach name="__ACTIONS_ITEMS__" id="{$item}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    public function tagLists($tag, $content)
    {
        // type,cate_id,is_home,thumb,num,order,item,page
        $cid     = $tag['cid'] ?? '';
        $tid     = $tag['tid'] ?? '';
        $cate_id = $tag['cate_id'] ?? '';    //所属栏目
        $type    = $tag['type'] ?? '';    //属性
        $thumb   = $tag['thumb'] ?? 0;    //是否有缩略图
        $num     = $tag['num'] ?? 10;   //数量
        $order   = $tag['order'] ?? "sort asc,update_time desc"; //排序
        $is_home = $tag['is_home'] ?? 0; //是否首页
        $item    = $tag['item'] ?? 'vo';//循环变量名
        $key     = $tag['key'] ?? 'key';//循环key
        $page    = $tag['page'] = $tag['page'] ?? 0;//是否开启分页

        $parse = <<<parse
        <?php
            if(empty(\$cid)){
                \$cid = "";
            }
            if(empty(\$tid)){
                \$tid = "";
            }
            \$is_page = $page;
            \$__ACTIONS_ITEMS__ = (new plugin\huiblog\app\model\Article())->lists('{$cate_id}','{$type}',{$thumb},{$num},'{$order}','{$is_home}','',\$cid,\$tid);
            if(\$__ACTIONS_ITEMS__->isEmpty() === true){
                echo "<div id=\"no_data\">暂无数据</div>";
            }
        ?>
        {foreach name="__ACTIONS_ITEMS__" id="{$item}" key="{$key}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    public function tagSearch($tag, $content)
    {
        $cate_id = $tag['cate_id'] ?? '';    //所属栏目
        $type    = $tag['type'] ?? '';    //属性
        $thumb   = $tag['thumb'] ?? 0;    //是否有缩略图
        $num     = $tag['num'] ?? 10;   //数量
        $order   = $tag['order'] ?? "sort asc,update_time desc"; //排序
        $is_home = $tag['is_home'] ?? 0; //是否首页
        $item    = $tag['item'] ?? 'vo';//循环变量名
        $key     = $tag['key'] ?? 'key';//循环key
        $keyword = $tag['keyword'] ?? '';//搜索关键词
        $page    = $tag['page'] = $tag['page'] ?? 0;//是否开启分页

        $parse = <<<parse
        <?php
            \$is_page = $page;
            \$__ACTIONS_ITEMS__ = (new plugin\huiblog\app\model\Article())->lists('{$cate_id}','{$type}',{$thumb},{$num},'{$order}','{$is_home}',\$keyword);
            if(\$__ACTIONS_ITEMS__->isEmpty() === true){
                echo "<div id=\"no_data\">暂无数据</div>";
            }
        ?>
        {foreach name="__ACTIONS_ITEMS__" id="{$item}" key="{$key}"}
        {$content}
        {/foreach}
        parse;

        return $parse;
    }

    public function tagPages($tag)
    {
        $parse = <<<parse
        <?php
        //实例化分页类
        \$page_list = "";
        if(is_object(\$__ACTIONS_ITEMS__) && \$is_page){
            \$page_list = \$__ACTIONS_ITEMS__->render();
        }
        echo \$page_list;
        ?>
parse;

        return $parse;
    }

}
